package com.kidult.android.common.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;

public class PullToRefreshListView extends PullToRefreshBase<ListView> {

    private OnScrollListener mOnScrollListener;
    private int mCurrentScrollState;

    public PullToRefreshListView(Context context) {
	super(context);
    }

    public PullToRefreshListView(Context context, AttributeSet attrs) {
	super(context, attrs);
    }

    // public PullToRefreshListView(Context context, AttributeSet attrs, int
    // defStyle) {
    // super(context, attrs, defStyle);
    // }

    public void setOnScrollListener(AbsListView.OnScrollListener listener) {
	mOnScrollListener = listener;
    }

    private class DefaultOnScrollListener implements AbsListView.OnScrollListener {

	// @Override
	// public void onScroll(AbsListView view, int firstVisibleItem, int
	// visibleItemCount, int totalItemCount) {
	// /**
	// * ListView为SCROLL_STATE_TOUCH_SCROLL状态(按着不放滚动中)并且刷新状态不为REFRESHING
	// * a. 刷新对应的item可见时，若刷新layout高度超出范围，则置刷新状态为RELEASE_TO_REFRESH；
	// * 若刷新layout高度低于高度范围，则置刷新状态为DROP_DOWN_TO_REFRESH b.
	// * 刷新对应的item不可见，重置header ListView为SCROLL_STATE_FLING状态(松手滚动中) a.
	// * 若刷新对应的item可见并且刷新状态不为REFRESHING，设置position为1的(即第二个)item可见 b.
	// * 若反弹回来，设置position为1的(即第二个)item可见
	// */
	// mCurrentPosition = judgeCurrentPosition();
	// if (mCurrentScrollState == SCROLL_STATE_TOUCH_SCROLL && mCurrentState
	// != RefreshStatusEnum.REFRESHING) {
	// if (mCurrentMode.canPullDown() && firstVisibleItem == 0) {
	// Log.i("header.getBottom()", header.getBottom() + "");
	// Log.i("header.mOriginalHeight + mHeightLimit", header.mOriginalHeight
	// + mHeightLimit + "");
	// if (header.getBottom() >= header.mOriginalHeight + mHeightLimit) {
	// Log.i("header ", "setStatusReleaseToRefresh()");
	// setStatusReleaseToRefresh();
	// } else if (header.getBottom() < header.mOriginalHeight +
	// mHeightLimit) {
	// Log.i("header", "setStatusPullToRefresh()");
	// setStatusPullToRefresh();
	// }
	// } else if (mCurrentMode.canPullUp()
	// && mPullableView.getLastVisiblePosition() == mPullableView.getCount()
	// - 1) {
	// Log.i("footer.getTop()", footer.getTop() + "");
	// Log.i("mPullableView.getHeight() - footer.mOriginalHeight - mHeightLimit",
	// mPullableView.getHeight() - footer.mOriginalHeight - mHeightLimit +
	// "");
	// if (footer.getTop() < mPullableView.getHeight() -
	// footer.mOriginalHeight - mHeightLimit) {
	// Log.i("footer", "setStatusReleaseToRefresh()");
	// setStatusReleaseToRefresh();
	// } else if (footer.getTop() >= mPullableView.getHeight() -
	// footer.mOriginalHeight - mHeightLimit) {
	// Log.i("footer", "setStatusPullToRefresh()");
	// setStatusPullToRefresh();
	// } else if (footer.getTop() == mPullableView.getHeight()) {
	// footer.setStatusClickToRefresh();
	// }
	// } else {
	// setStatusClickToRefresh();
	// }
	// }
	// if (mOnScrollListener != null) {
	// mOnScrollListener.onScroll(view, firstVisibleItem, visibleItemCount,
	// totalItemCount);
	// }
	// }

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
	    /**
	     * ListView为SCROLL_STATE_TOUCH_SCROLL状态(按着不放滚动中)并且刷新状态不为REFRESHING
	     * a. 刷新对应的item可见时，若刷新layout高度超出范围，则置刷新状态为RELEASE_TO_REFRESH；
	     * 若刷新layout高度低于高度范围，则置刷新状态为DROP_DOWN_TO_REFRESH b.
	     * 刷新对应的item不可见，重置header ListView为SCROLL_STATE_FLING状态(松手滚动中) a.
	     * 若刷新对应的item可见并且刷新状态不为REFRESHING，设置position为1的(即第二个)item可见 b.
	     * 若反弹回来，设置position为1的(即第二个)item可见
	     */
	    mCurrentPosition = judgeCurrentPosition();
	    if (mCurrentScrollState == SCROLL_STATE_TOUCH_SCROLL && mCurrentState != RefreshStatusEnum.REFRESHING) {
		if (mCurrentMode.canPullDown() && firstVisibleItem == 0) {
		    Log.i("header.getBottom()", header.getBottom() + "");
		    Log.i("header.mOriginalHeight + mHeightLimit", header.mOriginalHeight + mHeightLimit + "");
		    if (header.getBottom() >= header.mOriginalHeight + mHeightLimit) {
			Log.i("header ", "setStatusReleaseToRefresh()");
			setStatusReleaseToRefresh();
		    } else if (header.getBottom() < header.mOriginalHeight + mHeightLimit) {
			Log.i("header", "setStatusPullToRefresh()");
			setStatusPullToRefresh();
		    }
		} else if (mCurrentMode.canPullUp()
			&& mPullableView.getLastVisiblePosition() == mPullableView.getCount() - 1) {
		    Log.i("footer.getTop()", footer.getTop() + "");
		    Log.i("mPullableView.getHeight() - footer.mOriginalHeight - mHeightLimit",
			    mPullableView.getHeight() - footer.mOriginalHeight - mHeightLimit + "");
		    if (footer.getTop() < mPullableView.getHeight() - footer.mOriginalHeight - mHeightLimit) {
			Log.i("footer", "setStatusReleaseToRefresh()");
			setStatusReleaseToRefresh();
		    } else if (footer.getTop() >= mPullableView.getHeight() - footer.mOriginalHeight - mHeightLimit) {
			Log.i("footer", "setStatusPullToRefresh()");
			setStatusPullToRefresh();
		    } else if (footer.getTop() == mPullableView.getHeight()) {
			footer.setStatusClickToRefresh();
		    }
		} else {
		    setStatusClickToRefresh();
		}
	    }
	    if (mOnScrollListener != null) {
		mOnScrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
	    }
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
	    mCurrentScrollState = scrollState;
	    if (mOnScrollListener != null) {
		mOnScrollListener.onScrollStateChanged(view, scrollState);
	    }
	}

    }

    // class MyListViewTouchListener implements OnTouchListener {
    //
    // @Override
    // public boolean onTouch(View v, MotionEvent event) {
    // isBounceTop = false;
    // switch (event.getAction()) {
    // case MotionEvent.ACTION_DOWN:
    // mActionDownPointY = event.getY();
    // break;
    // case MotionEvent.ACTION_UP:
    // if (!isVerticalScrollBarEnabled()) {
    // setVerticalScrollBarEnabled(true);
    // }
    // if (mCurrentState != RefreshStatusEnum.REFRESHING) {
    // switch (mCurrentState) {
    // case CLICK_TO_REFRESH:
    // setStatusClickToRefresh();
    // break;
    // case RELEASE_TO_REFRESH:
    // if (mCurrentMode.canPullDown() && mPullableView.getFirstVisiblePosition()
    // == 0) {
    // onRefreshHeader();
    // } else if (mCurrentMode.canPullUp()
    // && mPullableView.getLastVisiblePosition() == mPullableView.getCount() -
    // 1) {
    // onRefreshFooter();
    // }
    // break;
    // case PULL_TO_REFRESH:
    // setStatusClickToRefresh();
    // setSecondPositionVisible();
    // break;
    // default:
    // break;
    // }
    // }
    // break;
    // case MotionEvent.ACTION_MOVE:
    // if (mCurrentScrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
    // if (mCurrentMode.canPullDown() && mPullableView.getFirstVisiblePosition()
    // == 0) {
    // adjustHeaderPadding(event);
    // } else if (mCurrentMode.canPullUp()
    // && mPullableView.getLastVisiblePosition() == mPullableView.getCount() -
    // 1) {
    // adjustFooterPadding(event);
    // }
    // }
    // break;
    // default:
    // break;
    // }
    // return false;
    // }
    // }

    // @Override
    // public boolean onTouchEvent(MotionEvent event) {
    // Log.i("TouchEvent", event.getAction() + "");
    // Log.i("OnTouchEvent", "onTouchEvent invoke");
    // isBounceTop = false;
    // switch (event.getAction()) {
    // case MotionEvent.ACTION_DOWN:
    // mActionDownPointY = event.getY();
    // break;
    // case MotionEvent.ACTION_MOVE:
    // if (mCurrentMode.canPullUp() && mPullableView.getLastVisiblePosition() ==
    // mPullableView.getCount() - 1) {
    // adjustFooterPadding(event);
    // } else if (mCurrentMode.canPullDown() &&
    // mPullableView.getFirstVisiblePosition() == 0) {
    // adjustHeaderPadding(event);
    // }
    // break;
    // case MotionEvent.ACTION_UP:
    // if (!isVerticalScrollBarEnabled()) {
    // setVerticalScrollBarEnabled(true);
    // }
    // if (mPullableView.getFirstVisiblePosition() == 0
    // || mPullableView.getLastVisiblePosition() == mPullableView.getCount() - 1
    // && mCurrentState != RefreshStatusEnum.REFRESHING) {
    // switch (mCurrentState) {
    // case CLICK_TO_REFRESH:
    // setStatusClickToRefresh();
    // break;
    // case RELEASE_TO_REFRESH:
    // if (mCurrentMode.canPullDown() && mPullableView.getFirstVisiblePosition()
    // == 0) {
    // onRefreshHeader();
    // } else if (mCurrentMode.canPullUp()
    // && mPullableView.getLastVisiblePosition() == mPullableView.getCount() -
    // 1) {
    // onRefreshFooter();
    // }
    // break;
    // case PULL_TO_REFRESH:
    // setStatusClickToRefresh();
    // setSecondPositionVisible();
    // break;
    // default:
    // break;
    // }
    // }
    // break;
    // }
    // return super.onTouchEvent(event);
    // }

    @Override
    protected ListView createPullableView(Context context) {
	ListView lv = new ListView(context);
	// switch (mCurrentMode) {
	// case PULL_DOWN_TO_REFRESH:
	// header = createLoadingLayout(context, FlipLoadingLayoutMode.HEADER);
	// lv.addHeaderView(header);
	// break;
	// case PULL_UP_TO_REFRESH:
	// footer = createLoadingLayout(context, FlipLoadingLayoutMode.FOOTER);
	// lv.addFooterView(footer);
	// break;
	// case BOTH:
	// header = createLoadingLayout(context, FlipLoadingLayoutMode.HEADER);
	// footer = createLoadingLayout(context, FlipLoadingLayoutMode.FOOTER);
	// lv.addHeaderView(header);
	// lv.addFooterView(footer);
	// default:
	// break;
	// }
	lv.setId(android.R.id.list);
	// lv.setOnScrollListener(new DefaultOnScrollListener());
	// lv.setOnTouchListener(new MyListViewTouchListener());
	return lv;
    }

    @Override
    protected com.kidult.android.common.view.PullToRefreshBase.CurrentPositionEnum judgeCurrentPosition() {
	if (null != mPullableView)
	    return mPullableView.getFirstVisiblePosition() == 0 ? CurrentPositionEnum.TOP : (mPullableView
		    .getLastVisiblePosition() == mPullableView.getCount() - 1 ? CurrentPositionEnum.BOTTOM
		    : CurrentPositionEnum.MIDDLE);
	return CurrentPositionEnum.MIDDLE;
    }
}
